package top.yudoge.dogeeditorserverj.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.yudoge.dogeeditorserverj.db.*;
import top.yudoge.dogeeditorserverj.db.config.SQLDataSource;
import top.yudoge.dogeeditorserverj.repository.DataBaseMetaDataRepository;

import java.util.List;

@Slf4j
@Service
public class DataBaseMetaDataService {

    private DataBaseMetaDataRepository dataBaseMetaDataRepository;

    public DataBaseMetaDataService(
            DataBaseMetaDataRepository repository,
            SQLDataSourceConnectorHolder connectorHolder
    ) {
        this.dataBaseMetaDataRepository = repository;
    }

    public void refresh(String dsName) {
        log.debug("refresh datasource {}", dsName);
        dataBaseMetaDataRepository.refresh(dsName);
    }

    public void refresh(String dsName, String dbName) {
        log.debug("refresh database {}.{}", dsName, dbName);
        dataBaseMetaDataRepository.refresh(dsName, dbName);
    }

    public void refresh(String dsName, String dbName, String schemaName) {
        log.debug("refresh schema {}.{}.{}", dsName, dbName, schemaName);
        dataBaseMetaDataRepository.refresh(dsName, dbName, schemaName);
    }

    public void refresh(String dsName, String dbName, String schemaName, String tableName) {
        log.debug("refresh table {}.{}.{}.{}", dsName, dbName, schemaName, tableName);
        dataBaseMetaDataRepository.refresh(dsName, dbName, schemaName, tableName);
    }

    public List<SQLDataSource> getDataSources() {
        log.debug("get datasources");
        return dataBaseMetaDataRepository.getDataSources();
    }

    public List<String> getDatabase(String dsName) {
        log.debug("get database {}", dsName);
        return dataBaseMetaDataRepository.getDatabase(dsName);
    }

    public List<String> getSchema(String dsName, String dbName) {
        log.debug("get schema {}.{}", dsName, dbName);
        return dataBaseMetaDataRepository.getSchema(dsName, dbName);
    }

    public List<Table> getTable(String dsName, String dbName, String schemaName) {
        log.debug("get table {}.{}.{}", dsName, dbName, schemaName);
        return dataBaseMetaDataRepository.getTable(dsName, dbName, schemaName);
    }

    public List<Column> getColumns(String dsName, String dbName, String schemaName, String tableName) {
        log.debug("get columns {}.{}.{}.{}", dsName, dbName, schemaName, tableName);
        return dataBaseMetaDataRepository.getColumns(dsName, dbName, schemaName, tableName);
    }

    public List<Index> getIndex(String dsName, String dbName, String schemaName, String tableName) {
        log.debug("get indices {}.{}.{}.{}", dsName, dbName, schemaName, tableName);
        return dataBaseMetaDataRepository.getIndex(dsName, dbName, schemaName, tableName);
    }
}
